# -*- coding:utf-8 -*-
import os

from common import common_method


def del_file(target_directory):
    """
    删除指定目录下的所有文件，非目录
    """
    # 列出目录下的所有文件
    file_list = os.listdir(target_directory)

    # 遍历并删除每个文件
    for file_name in file_list:
        file_path = os.path.join(target_directory, file_name)
        try:
            if os.path.isfile(file_path):  # 确保它是一个文件而不是子目录
                os.remove(file_path)
                print(f"已删除文件：{file_path}")
        except Exception as e:
            print(f"删除文件 {file_path} 时出错：{e}")


def delete_files_and_directories(directory):
    """
    删除指定目录下的所有文件及目录
    """
    for filename in os.listdir(directory):
        file_path = os.path.join(directory, filename)
        try:
            if os.path.isfile(file_path):
                os.remove(file_path)
                print(f"已删除文件：{file_path}")
            else:
                delete_files_and_directories(file_path)
                os.rmdir(file_path)
                print(f"已删除目录：{file_path}")
        except Exception as e:
            print(f"删除文件 {file_path} 时出错：{e}")
        print("删除成功")


def has_file_or_dir(directory_path):
    """
    是否有文件或目录
    """
    has = len(os.listdir(directory_path)) > 0
    print("有文件或目录" if has else "没有文件或目录")
    return has


if __name__ == "__main__":
    if has_file_or_dir(common_method.no_upload_path()):
        delete_files_and_directories(common_method.no_upload_path())
